/*
 * Copyright (C) 2018 Logos Technology Ltd.
 */

package com.logos.common.base.operation;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.DrawableRes;

/**
 * @author Young Ken.
 */
public class TitleOperation implements ITitleOperation, View.OnClickListener {

    private static final String TAG_LEFT_IMAGE = "leftImage";
    private static final String TAG_TITLE_NAME = "titleName";
    private static final String TAG_RIGHT_LAYOUT = "rightLayout";
    private static final String TAG_RIGHT_TEXT = "rightText";
    private static final String TAG_RIGHT_IMAGE = "rightImage";
    private int mSourceId = 0;
    private Context mContext;
    private RelativeLayout mTitleLayout;
    private ImageView mLeftImage;
    private TextView mTitleView;
    private RelativeLayout mRightLayout;
    private ImageView mRightImage;
    private TextView mRightText;

    private ITitleLeftClick mLeftClick;
    private IRightLayoutClick mRightClick;

    /**
     * 构造函数
     * @param context 上下文
     * @param sourceId 资源id.
     */
    public TitleOperation(Context context, int sourceId) {

        if (context == null || sourceId == 0) {
            throw new IllegalArgumentException("parameter is null");
        }
        mContext = context;
        mSourceId = sourceId;

        splitView();
    }

    /**
     * 添加父布局
     * @param viewGroup 父布局.
     */
    public void addParent(ViewGroup viewGroup) {

        viewGroup.addView(mTitleLayout, new ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
    }

    @Override
    public ImageView getRightImage() {

        return mRightImage;
    }

    @Override
    public TextView getRightText() {

        return mRightText;
    }

    private void splitView() {
        LayoutInflater inflater = (LayoutInflater) mContext
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mTitleLayout = (RelativeLayout) inflater.inflate(mSourceId, null);

        mLeftImage = (ImageView) mTitleLayout.findViewWithTag(TAG_LEFT_IMAGE);
        mTitleView = (TextView) mTitleLayout.findViewWithTag(TAG_TITLE_NAME);
        mRightLayout = (RelativeLayout) mTitleLayout.findViewWithTag(TAG_RIGHT_LAYOUT);
        mRightText = (TextView) mTitleLayout.findViewWithTag(TAG_RIGHT_TEXT);
        mRightImage = (ImageView) mTitleLayout.findViewWithTag(TAG_RIGHT_IMAGE);

        if (mLeftImage == null || mRightLayout == null || mTitleView == null
            || mRightText == null || mRightImage == null) {

            throw new RuntimeException("The layout file is not tag");
        }

        mLeftImage.setOnClickListener(this);
        mRightLayout.setOnClickListener(this);
    }

    @Override
    public void setTitleName(String titleName) {

        if (mTitleView != null) {
            mTitleView.setText(titleName);
        }
    }

    @Override
    public void addRightImage(@DrawableRes int resId) {

        if (mRightImage != null) {
            mRightImage.setVisibility(View.VISIBLE);
            mRightImage.setImageResource(resId);
        }
    }

    @Override
    public void addRightText(String rightTextName) {

        if (mRightText != null) {
            mRightText.setVisibility(View.VISIBLE);
            mRightText.setText(rightTextName);
        }

    }

    @Override
    public void setLeftImageClick(ITitleLeftClick click) {

        mLeftClick = click;
    }

    @Override
    public void setRightLayoutClick(IRightLayoutClick click) {

        mRightClick = click;
    }

    @Override
    public void onClick(View view) {

        String tag = (String) view.getTag();

        if (tag.equals(TAG_LEFT_IMAGE)) {
            if (mLeftClick != null) {
                mLeftClick.titleLeftClick();
            }
        } else if (tag.equals(TAG_RIGHT_LAYOUT)) {
            if (mRightClick != null) {
                mRightClick.rightLayoutClick();
            }
        }
    }
}
